//
//  NFSDiscoverViewController.m
//  NeedForSpeed
//
//  Created by dlios on 15/10/15.
//  Copyright (c) 2015年 Avenger. All rights reserved.
//

#import "NFSDiscoverViewController.h"

#import "BaseHeader.h"

#import "NFSLoopPlayView.h"

#import "NFSLoopPlayComeingViewController.h"

#import "NFSCarsPriceView.h"

#import "UIButton+WebCache.h"

#import "NFSShopCarsViewController.h"

#import "NFSLoveCarsViewController.h"

#import "NFSRadioViewController.h"

#import "NFSComparisonViewController.h"

#import "NFSShopPlanViewController.h"

#import "NFSTwoHandCarViewController.h"

#import "NFSmoreCarsViewController.h"

@interface NFSDiscoverViewController ()<NFSLoopPlayDelegate,NFSCarPricesDelegate>

@property(nonatomic,strong)UIScrollView *mainScrollView;

@end

@implementation NFSDiscoverViewController
-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:247./255. green:247./255. blue:247./255. alpha:1]];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.autoresizesSubviews = NO;
    UIColor *color =[UIColor grayColor];
    NSDictionary *dict = [NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    [self.tabBarController.tabBar setHidden:NO];
    self.navigationController.navigationBar.translucent = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self creatMainScrollView];//视图背景是一个整体是一个ScrollView
    [self creatLoopPlayView];//轮播图
    [self createButtonsView];//各种按钮集合视图
    [self createCarPriceView];
}
#pragma mark - 主界面collection
- (void)creatMainScrollView
{
    _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:_mainScrollView];
    [_mainScrollView setBounces:NO];
    [_mainScrollView setContentSize:CGSizeMake(0, SCREEN_HEIGHT *3 / 2)];
    [_mainScrollView setBackgroundColor:[UIColor colorWithRed:244./255. green:244./255. blue:244./255. alpha:1]];
 
}
#pragma mark - 轮播图
- (void)creatLoopPlayView
{
    NFSLoopPlayView *loop = [[NFSLoopPlayView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/6)];
    [_mainScrollView addSubview:loop];
    [loop setBackgroundColor:[UIColor redColor]];
    loop.NFSLoopPlayDeldgate =self;
}

#pragma mark - 界面中部button
- (void)createButtonsView
{
    UIView *ButtonsView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 6 , SCREEN_WIDTH, SCREEN_HEIGHT *2 / 6)];
    [_mainScrollView addSubview:ButtonsView];
    [ButtonsView setBackgroundColor:[UIColor whiteColor]];
    
    for (int i = 0; i < 4; i ++) {
        UIButton *clickButton = [[UIButton alloc] init];
        [clickButton setFrame:CGRectMake(SCREEN_WIDTH / 4 / 2 * i +  SCREEN_WIDTH / 4 / 2 / 2  * (1 +i *2), SCREEN_HEIGHT / 6 /2 /2 , SCREEN_WIDTH / 4 / 2, SCREEN_HEIGHT / 6 / 2)];
        [clickButton setTag:i];
        [clickButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0,SCREEN_HEIGHT/48, 0)];
        [clickButton setTitleEdgeInsets:UIEdgeInsetsMake(SCREEN_HEIGHT / 6 / 2 - SCREEN_HEIGHT / 48, 0, 0, 0)];
        [clickButton setImage:[UIImage imageNamed:@"discoveryfunction_6@2x.png"] forState:UIControlStateNormal];
        //clickButton.imageView.backgroundColor = [UIColor redColor];
        
        [clickButton setTitle:@"SDFGHJK" forState:UIControlStateNormal];
//        [clickButton setBackgroundColor:[UIColor whiteColor]];
        [clickButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//        clickButton.titleLabel.font = [UIFont systemFontOfSize:14.];
        
        [ButtonsView addSubview: clickButton];
        [self changeButton:clickButton];
        
        [clickButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    for (int i = 0; i < 4; i ++) {
        UIButton *secClickButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [secClickButton setFrame:CGRectMake(SCREEN_WIDTH / 4 / 2 * i +  SCREEN_WIDTH / 4 / 2 / 2  * (1 +i *2), SCREEN_HEIGHT / 6 /2 /2 + SCREEN_HEIGHT / 6 , SCREEN_WIDTH / 4 / 2, SCREEN_HEIGHT / 6 / 2)];
        [secClickButton setTag:i + 4];
        [secClickButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0,SCREEN_HEIGHT/48, 0)];
        [secClickButton setTitleEdgeInsets:UIEdgeInsetsMake(secClickButton.frame.size.height - SCREEN_HEIGHT / 48, 0, 0, 0 )];
        
        [secClickButton setTitle:@"SDFGHJK" forState:UIControlStateNormal];

        
        
        [secClickButton setBackgroundColor:[UIColor whiteColor]];
        [secClickButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        secClickButton.titleLabel.font = [UIFont systemFontOfSize:14.];
        
        [ButtonsView addSubview: secClickButton];
        [self changeButton:secClickButton];
        
        [secClickButton addTarget:self action:@selector(secButton:) forControlEvents:UIControlEventTouchUpInside];
    }


}
- (void)clickButton:(UIButton *)sender
{
// 电台
    NFSRadioViewController *radioView = [[NFSRadioViewController alloc]init];
// comparison对比
    NFSComparisonViewController *comparisonView = [[NFSComparisonViewController alloc]init];
//购车计划
    NFSShopPlanViewController *shopCarsPlan = [[NFSShopPlanViewController alloc]init];
//违规
    NFSShopCarsViewController *shopView =[[NFSShopCarsViewController alloc]init];
    switch (sender.tag) {
        case 0:
            [self.navigationController pushViewController:radioView animated:YES];
            [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
            self.navigationController.navigationBar.translucent = YES;
            [self.tabBarController.tabBar setHidden:YES];
            break;
        case 1:
            [self.navigationController pushViewController:comparisonView animated:YES];
            self.navigationController.navigationBar.translucent =YES;
            [self.tabBarController.tabBar setHidden:YES];
            
            
            break;
        case 2:
            [self.navigationController pushViewController:shopCarsPlan animated:YES];
            self.navigationController.navigationBar.translucent = YES;
            [self.tabBarController.tabBar setHidden:YES];
            break;
        case 3:
            
            [self.navigationController pushViewController:shopView animated:YES];
            shopView.address = @"http://wz.m.autohome.com.cn/?from=AutohomeApp&pvareaid=103171";
            break;

            
        default:
            break;
    }
}
- (void)secButton :(UIButton *)sender
{
    NFSLoveCarsViewController *loveCarsView = [[NFSLoveCarsViewController alloc]init];
    
    NFSTwoHandCarViewController *twoHandCarView = [[NFSTwoHandCarViewController alloc]init];
    switch (sender.tag) {
        case 4:
            [self.navigationController pushViewController:twoHandCarView animated:YES];
            self.navigationController.navigationBar.translucent =YES;
            [self.tabBarController.tabBar setHidden:YES];

            break;
        case 5:
            [self.navigationController pushViewController:loveCarsView animated:YES];
            loveCarsView.address = @"http://m.che168.com/pinggu/?buymark=oaha10027&sellmark=oaha10026&appversionbuy=2.08V&appversionsell=2.04V&hideheadfoot=1&hidead=1&pvareaid=101375";
            break;
        case 6:
            [self.navigationController pushViewController:loveCarsView animated:YES];
            loveCarsView.address = @"http://m.mall.autohome.com.cn/index.html?isapp=1";
            break;
        case 7:
            [self.navigationController pushViewController:loveCarsView animated:YES];
            loveCarsView.address = @"http://diandong.m.autohome.com.cn/#pvareaid=103843";
            break;
            
            
        default:
            break;
    }

}


#pragma mark 对每个按钮进行相应处理
- (void)changeButton:(UIButton *)sender
{
    
    NSURL *videoUrl = [NSURL URLWithString:@"http://x.autoimg.cn/app/image/banner/201509252006224849892.jpg"];
    NSURL *contrastUrl =[NSURL URLWithString:@"http://x.autoimg.cn/app/image/banner/201509252007266404209.jpg"];
    NSURL *shopUrl =[NSURL URLWithString:@"http://x.autoimg.cn/app/image/banner/201509252008106554704.jpg"];
    NSURL *rulesUrl =[NSURL URLWithString:@"http://x.autoimg.cn/app/image/banner/201509252008327826510.jpg"];
    NSURL *twoHandCarUrl =[NSURL URLWithString:@"http://x.autoimg.cn/app/image/banner/201509252009091103210.jpg"];
    NSURL *loveCarUrl =[NSURL URLWithString:@"http://baojia0.autoimg.cn/bj/2015101211/1648455169_1444620579271.png"];
    NSURL *carsStore =[NSURL URLWithString:@"http://x.autoimg.cn/app/image/banner/201509252007477963596.jpg"];
    NSURL *electrombileUrl =[NSURL URLWithString:@"http://baojia0.autoimg.cn/bj/2015101211/367002909_1444621035237.png"];
    switch (sender.tag) {
        case 0:
            [sender sd_setImageWithURL:videoUrl forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
//                [sender setTitle:@"汽车电台" forState:UIControlStateNormal];
                 sender.titleLabel.text = @"!@#$%^&";
            
            break;
        case 1:
            [sender sd_setImageWithURL:contrastUrl forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
//            [sender setTitle:@"车型对比" forState:UIControlStateNormal];
            break;
        case 2:
            [sender sd_setImageWithURL:shopUrl forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
//            [sender setTitle:@"购车计算" forState:UIControlStateNormal];
            
            break;

        case 3:
            [sender sd_setImageWithURL:rulesUrl forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
//            [sender setTitle:@"违章查询" forState:UIControlStateNormal];
            break;

        case 4:
            [sender sd_setImageWithURL:twoHandCarUrl forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
//            [sender setTitle:@"找二手车" forState:UIControlStateNormal];
            break;

        case 5:
            [sender sd_setImageWithURL:loveCarUrl forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
//            [sender setTitle:@"爱车估值" forState:UIControlStateNormal];
            break;

        case 6:
            [sender sd_setImageWithURL:carsStore forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
//            [sender setTitle:@"车商城" forState:UIControlStateNormal];
            break;

        case 7:
            [sender sd_setImageWithURL:electrombileUrl forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
//            [sender setTitle:@"电动车之家" forState:UIControlStateNormal];
            
            break;
            
        default:
            break;
    }
    
    sender.titleLabel.text = @"!@#$%^&";

}
#pragma mark - 创建collection
- (void)createCarPriceView
{
    UIView *CarPriceView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT *3 / 6 + SCREEN_HEIGHT/30, SCREEN_WIDTH, SCREEN_HEIGHT *3 / 6)];
    [CarPriceView setBackgroundColor:[UIColor whiteColor]];
    
    UIButton *moreCarsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreCarsButton setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/ 15)];
    [moreCarsButton setBackgroundColor:[UIColor colorWithRed:250./255. green:250./255. blue:250./255. alpha:1]];
    [moreCarsButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [moreCarsButton setTitle:@"一键点击   查看更多" forState:UIControlStateNormal];
    [moreCarsButton addTarget:self action:@selector(moreRadio) forControlEvents:UIControlEventTouchUpInside];
    [CarPriceView addSubview:moreCarsButton];
    [_mainScrollView addSubview:CarPriceView];
    
    NFSCarsPriceView *carspriceView = [[NFSCarsPriceView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/15, SCREEN_WIDTH, SCREEN_HEIGHT *2 / 3 )];
    [CarPriceView addSubview:carspriceView];
    carspriceView.NFSCarPrice =self;
}
- (void)moreRadio
{
    NFSmoreCarsViewController *moreCarsView = [[NFSmoreCarsViewController alloc]init];
    moreCarsView.address = @"http://m.mall.autohome.com.cn/list/2-0-0-0-0-0-0-0-0-1.html?isapp=1";
    [self.navigationController pushViewController:moreCarsView animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 轮播图协议方法
- (void)NFSLoopPlayDelegate:(NFSLoopPlayModel *)model
{
    NFSLoopPlayComeingViewController *loopPlayComeingView = [[NFSLoopPlayComeingViewController alloc]init];
    loopPlayComeingView.address = model.url;
    [self.navigationController pushViewController:loopPlayComeingView animated:YES];
}
#pragma mark - 汽车价格协议方法
- (void)NFSCarPricesDelegate:(NFSCarsPriceModel *)model
{
    NFSLoopPlayComeingViewController *loopPlayComeingView = [[NFSLoopPlayComeingViewController alloc]init];
    loopPlayComeingView.address = model.murl;
    [self.navigationController pushViewController:loopPlayComeingView animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
